//
//  NSString+ZDMD5Secret.m
//  SSSAllRollAgency
//
//  Created by Darker on 2017/3/22.
//  Copyright © 2017年 Darker. All rights reserved.
//

#import "NSString+ZDMD5Secret.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (ZDMD5Secret)


+ (NSString *)md5HexDigest:(NSString*)input
{
    const char *str = [input UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(str, (int)strlen(str), result);
    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH];
    for(int i = 0; i< CC_MD5_DIGEST_LENGTH; i++) {
        [ret appendFormat:@"%02X",result[i]];
    }
    return ret;
}

- (NSString *) md5HexDigest
{
    const char *original_str = [self UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(original_str, (int)strlen(original_str), result);
    NSMutableString *hash = [NSMutableString string];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [hash appendFormat:@"%02X", result[i]];
    return [hash lowercaseString];
}

///**
// *  sha1加密
// *
// *  @return 获取加密后的字符串
// */
//- (NSString *)sha1 {
//    //  将字符串转换成 C字符串
//    const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
//    //  C字符串转换成二进制数据
//    NSData *data = [NSData dataWithBytes:cstr length:self.length];
//
//    unsigned char digest[CC_SHA1_DIGEST_LENGTH];
//    CC_SHA1(data.bytes, (uint32_t)data.length, digest);
//
//    NSMutableString *encryptionString = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
//
//    for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
//        [encryptionString appendFormat:@"%02x", digest[i]];
//    }
//
//    return encryptionString;
//}
//
///**
// *  sha1+base64加密
// *
// *  @return 获取加密后的字符串
// */
//- (NSString *)sha1_base64 {
//
//    const char *cStr = [self cStringUsingEncoding:NSUTF8StringEncoding];
//    NSData *data = [NSData dataWithBytes:cStr length:self.length];
//
//    unsigned char digest[CC_SHA1_DIGEST_LENGTH];
//    CC_SHA1(data.bytes, (uint32_t)data.length, digest);
//
//    NSData *base64 = [[NSData alloc] initWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
//    base64 = [GTMBase64 encodeData:base64];
//
//    NSString *encryptionString = [[NSString alloc] initWithData:base64 encoding:NSUTF8StringEncoding];
//
//    return encryptionString;
//}
//
///**
// *  md5+base64加密
// *
// *  @return 获取加密后的字符串
// */
//- (NSString *)md5_base64 {
//
//    const char *cStr = [self UTF8String];
//    unsigned char digest[CC_MD5_DIGEST_LENGTH];
//    CC_MD5(cStr, (uint32_t)strlen(cStr), digest);
//
//    NSData *base64 = [[NSData alloc] initWithBytes:digest length:CC_MD5_DIGEST_LENGTH];
//    base64 = [GTMBase64 encodeData:base64];
//
//    NSString *encryptionString = [[NSString alloc] initWithData:base64 encoding:NSUTF8StringEncoding];
//
//    return encryptionString;
//}

-(BOOL)isBankCard:(NSString *)cardNumber{
    if(cardNumber.length==0){
        return NO;
    }
    NSString *digitsOnly = @"";
    char c;
    for (int i = 0; i < cardNumber.length; i++){
        c = [cardNumber characterAtIndex:i];
        if (isdigit(c)){
            digitsOnly =[digitsOnly stringByAppendingFormat:@"%c",c];
        }
    }
    int sum = 0;
    int digit = 0;
    int addend = 0;
    BOOL timesTwo = false;
    for (NSInteger i = digitsOnly.length - 1; i >= 0; i--){
        digit = [digitsOnly characterAtIndex:i] - '0';
        if (timesTwo){
            addend = digit * 2;
            if (addend > 9) {
                addend -= 9;
            }
        }
        else {
            addend = digit;
        }
        sum += addend;
        timesTwo = !timesTwo;
    }
    int modulus = sum % 10;
    return modulus == 0;
}


@end
